<?php
// 分页处理

class page {
	// 起始行数
	public $firstRow	;
	// 列表每页显示行数
	public $listRows	;
	// 页数跳转时要带的参数
	public $parameter  ;
	// 分页总页面数
	protected $totalPages  ;
	// 总行数
	protected $totalRows  ;
	// 当前页数
	protected $nowPage    ;
	// 分页栏每页显示的页数
	protected $rollPage   ;
	// 分页显示定制
	protected $config  =	array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'末页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %firstPage%  %prePage%  %linkPage%  %nextPage%  %lastPage%');

	/**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
	public function __construct($totalRows,$listRows='',$parameter=array()) {
		$this->totalRows = $totalRows;
		$this->parameter = $this->setParameter($parameter);

		$this->rollPage = C('PAGE_ROLLPAGE');
		$this->listRows = !empty($listRows)?$listRows:C('PAGE_LISTROWS');
		$this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
		$this->nowPage  = !empty($_GET[C('VAR_PAGE')])?$_GET[C('VAR_PAGE')]:1;
		if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
			$this->nowPage = $this->totalPages;
		}
		$this->firstRow = $this->listRows*($this->nowPage-1);
	}

	public function setConfig($name,$value) {
		if(isset($this->config[$name])) {
			$this->config[$name] = $value;
		}
	}



	/**
     +----------------------------------------------------------
     * 分页显示输出
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
	public function show() {
		$firstNum = 1;
		$preNum = $this->nowPage - 1;
		$nextNum = $this->nowPage + 1;
		$lastNum = $this->totalPages;
		$linkPage = $this->pageNum();

		if ($this->nowPage == 1) {
			$firstPage = '<span>'.$this->config['first'].'</span>';
			$prePage = '<span>'.$this->config['prev'].'</span>';
		}else{
			$firstPage = '<a href="'.$this->getUrl($firstNum).'">'.$this->config['first'].'</a>';
			$prePage = '<a href="'.$this->getUrl($preNum).'">'.$this->config['prev'].'</a>';
		}
		$urlStr .= $pageNum;
		if ($this->totalPages == $this->nowPage || $this->totalPages == 0) {
			$nextPage = '<span>'.$this->config['next'].'</span>';
			$lastPage = '<span>'.$this->config['last'].'</span>';
		}else{
			$nextPage = '<a href="'.$this->getUrl($nextNum).'">'.$this->config['next'].'</a>';
			$lastPage = '<a href="'.$this->getUrl($lastNum).'">'.$this->config['last'].'</a>';
		}

		$pageStr	 =	 str_replace(
		array('%header%','%nowPage%','%totalRow%','%totalPage%','%firstPage%','%prePage%','%linkPage%','%nextPage%','%lastPage%'),
		array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$firstPage,$prePage,$linkPage,$nextPage,$lastPage),$this->config['theme']);
		return $pageStr;
	}

	//对页数进行循环显示，如 1 2 3 4 5
	public function pageNum(){

		//循环显示处理
		$page = $this->nowPage;//当前页
		$rollPage = $this->rollPage;//每次分页数
		$totalPages = $this->totalPages;//总页数
		$start = $page-$rollPage;
		$end = $page+$rollPage-1;
		//处理分页开始数
		if ($start <= 0) {
			$end = $end + abs($start) + 1;
			$start = 1;
		}
		//处理分页结束数
		if ($end > $totalPages) {
			$num = $end - $totalPages;//差数
			$end = $end - $num;
			$start = $start - $num;
			if ($start <= 0) {
				$start = 1;
			}
		}
		$urlStr = "";
		for ($i=$start; $i<=$end; $i++){
			if ($i == $page) {
				$urlStr.='<span class="current">'.$i.'</span> ';
			}else{
				$urlStr.='<a href="'.$this->getUrl($i).'">'.$i.'</a> ';
			}
		}
		return $urlStr;
	}

	//得到url地址
	protected function getUrl($page){
		$depr = C('URL_PATHINFO_DEPR');
		$htm = C('URL_HTML_SUFFIX');
		$url = $this->parameter.$depr.C('VAR_PAGE').$depr.$page.$htm;
		return $url;
	}

	//设置当前网址
	protected function setParameter($parameter){		
		$depr = C('URL_PATHINFO_DEPR');
		
		// 如果开启了二级域名部署 则去掉url路径里面的分组参数
//		if(C('APP_MULTILEVELDOMAIN_DEPLOY_ON') && C('APP_MULTILEVELDOMAIN_DEPLOY')){
//			if ( defined('GROUP_NAME') ) {
//				$domain = trim(strtolower( GROUP_NAME ),$depr);
//				$deploys = (array)C('APP_MULTILEVELDOMAIN_DEPLOY');
//				$deploys = isset($deploys[$domain]) ? $deploys[$domain] : false;
//				if ($deploys) {
//					$_action = __APP__.'/'.MODULE_NAME.$depr.ACTION_NAME;
//				}
//			}
//		}
		if (empty($_action)) $_action = __ACTION__;
		
		//合并自定参数
		if (is_array($parameter)) {
			$new_get = array_merge($_GET,$parameter);
		}
		
		//去掉分页参数
		unset($new_get[C('VAR_PAGE')]);
		
		//组装分页URL地址
		foreach ($new_get as $key => $val){
			$url .= $depr.$key.$depr.$val;
		}
		$url = $_action.$url;
		return $url;
	}
}
?>